home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / RM_ALL.C < prev    next >
C/C++ Source or Header  |  1991-09-24  |  3KB  |  139 lines

  1. /*
  2. **  Remove all files and (optionally) subdirectories
  3. **
  4. **  public domain demo by Bob Stout
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <io.h>
  11. #include <dos.h>
  12. #include <ctype.h>
  13.  
  14. #define LAST_CHAR(str) (str[strlen(str) - 1])
  15. #define MAX_PATH 80
  16. #define SUCCESS  0
  17.  
  18. #ifdef __TURBOC__
  19.  #include <dir.h>
  20.  #define find_1st(n,a,b) (findfirst((n),(b),(a)))
  21.  #define find_nxt(b) (findnext(b))
  22.  #define find_t ffblk
  23.  #define name ff_name
  24.  #define attrib ff_attrib
  25.  #define _A_SUBDIR FA_DIREC
  26. #else
  27.  #include <direct.h>
  28.  #define find_1st(n,a,b) (_dos_findfirst((n),(a),(b)))
  29.  #define find_nxt(b) (_dos_findnext(b))
  30. #endif
  31.  
  32. /* Select one of the following - remove() is ANSI       */
  33.  
  34. #define rmfunc remove
  35. /* #define rmfunc unlink */
  36.  
  37. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  38. LOGICAL recurse = FALSE;
  39.  
  40. /*
  41. **  Clean all files from a directory
  42. */
  43.  
  44. void clean_dir(char *path)
  45. {
  46.       char rmpath[MAX_PATH], *rmfile;
  47.       struct find_t fbuf;
  48.  
  49.       strcpy(rmpath, path);
  50.       if ('\\' != LAST_CHAR(rmpath))
  51.             strcat(rmpath, "\\");
  52.       rmfile = &rmpath[strlen(rmpath)];
  53.       strcpy(rmfile, "*.*");
  54.       if (0 == find_1st(rmpath, 0, &fbuf)) do
  55.       {
  56.             strcpy(rmfile, fbuf.name);
  57.             rmfunc(rmpath);
  58.             printf("deleting %s\n", rmpath);
  59.       } while (0 == find_nxt(&fbuf));
  60. }
  61.  
  62. /*
  63. **  Process directories
  64. */
  65.  
  66. void do_dir(char *path)
  67. {
  68.       char search[MAX_PATH], new[MAX_PATH];
  69.       struct find_t ff;
  70.  
  71.       strcpy(search, path);
  72.       if ('\\' != LAST_CHAR(search))
  73.             strcat(search, "\\");
  74.       strcat(search, "*.*");
  75.       if (SUCCESS == find_1st(search, 0xff, &ff)) do
  76.       {
  77.             if (ff.attrib & _A_SUBDIR && '.' != *ff.name)
  78.             {
  79.                   strcat(strcat(strcpy(new, path), "\\"), ff.name);
  80.                   do_dir(new);
  81.             }
  82.       } while (SUCCESS == find_nxt(&ff));
  83.       clean_dir(path);
  84.       rmdir(path);
  85. }
  86.  
  87. /*
  88. **  Tell 'em they messed up
  89. */
  90.  
  91. void usage(LOGICAL errstat)
  92. {
  93.       if (errstat)
  94.             fputc('\a', stderr);
  95.       fputs("Usage: RM_ALL directory [...directory] [-r?]\n", stderr);
  96.       fputs("switches: -r Recurse subdirectories\n", stderr);
  97.       fputs("          -? Display help (this message)\n", stderr);
  98.       exit(errstat);
  99. }
  100.  
  101. /*
  102. **  RM_ALL - Deletes all files and (optionally) subdirectories
  103. */
  104.  
  105. int main(int argc, char *argv[])
  106. {
  107.       char rmpath[MAX_PATH], *rmfile;
  108.       int i;
  109.       LOGICAL found_dir = FALSE;
  110.       void (*clean_func)(char *) = clean_dir;
  111.  
  112.       for (i = 1; i < argc; ++i)          /* Check for switches         */
  113.       {
  114.             if (NULL == strchr("-/", *argv[i]))
  115.                   continue;               /* Assume it's a filename     */
  116.             switch (toupper(argv[i][1]))
  117.             {
  118.             case 'R':
  119.                   clean_func = do_dir;
  120.                   break;
  121.             case '?':
  122.                   usage(FALSE);
  123.                   break;
  124.             default:
  125.                   usage(ERROR);
  126.             }
  127.       }
  128.       for (i = 1; i < argc; ++i)          /* Scan filenames             */
  129.       {
  130.             if (strchr("/-", *argv[i]))
  131.                   continue;
  132.             found_dir = TRUE;
  133.             clean_func(argv[i]);
  134.       }
  135.       if (!found_dir)
  136.             usage(TRUE);
  137.       else  return 0;
  138. }
  139.